package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.form.MemberRegistForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-09
 */

@Controller
@RequestMapping("/api/ucenter/member")
public class ApiMemberController {
    @Autowired
    MemberService memberService;

    @Value("${ssss}")
    String ssss;
    //远程调用微服务
    @GetMapping("/order/{memberId}")
    @ResponseBody
    public R getMemberOrder(@PathVariable("memberId") String memberId){
        MemberDto memberDto = memberService.getOrderMember(memberId);
        return R.ok().data("data",memberDto);
    }

    @GetMapping("/getRegistNum")
    @ResponseBody
    public R getRegistNum(@RequestParam("day") String day){
        //根据日期获取注册数量
        Integer count = memberService.getgetRegistNumDay(day);
        return R.ok().data("data",count);//int类型会转成double类型
    }

    //个人中心：登录  注册  修改个人信息
    //1、注册请求
    @PostMapping("regist")
    @ResponseBody
    public R regist(@RequestBody MemberRegistForm member) {
        memberService.regist(member);
        return R.ok();
    }

    //登录
    @GetMapping("/login")
    @ResponseBody
    public R login(@RequestParam("mobile") String mobile,
                   @RequestParam("password") String password) {
        String token = memberService.login(mobile, password);
        return R.ok().data("data", token);
    }

    //解析token令牌
    @GetMapping("/auth/getuserinfo")
    @ResponseBody
    public R getUserInfo(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("data", jwtInfo);
    }

    //微信登录
    @GetMapping("weixin")
    public String wxlogin(HttpSession session) {
        String url = memberService.wxlogin(session);
        return url;
    }

    //微信登录回调
    @GetMapping("callback")
    public String callback(String code, String state, HttpSession session) {
        String url = memberService.wxCallback(code, state, session);
        return url;
    }
}

